package activity.example.administrator.livedemo;

import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.SurfaceView;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.baidu.recorder.api.LiveConfig;
import com.baidu.recorder.api.LiveSession;
import com.baidu.recorder.api.LiveSessionHW;
import com.baidu.recorder.api.LiveSessionSW;
import com.baidu.recorder.api.SessionStateListener;

public class MainActivity extends AppCompatActivity {
    private SurfaceView sfv;
    private Button btn;
    private LiveSession liveSession;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().hide();
        sfv = (SurfaceView) findViewById(R.id.ssv);
        btn = (Button) findViewById(R.id.btn);
        initRTMP();


    }

    private void initRTMP() {
        // 定义liveConfig
        LiveConfig liveConfig = new LiveConfig.Builder()
                .setCameraId(LiveConfig.CAMERA_FACING_FRONT)// 前置摄像头
                .setCameraOrientation(LiveConfig.ORIENTATION_PORTRAIT)// 竖屏
                .setVideoWidth(720).setVideoHeight(480)
                .setVideoFPS(15)// 帧率
                .setInitVideoBitrate(1200) // 码率
                .setAudioBitrate(64 * 1000)
                .setAudioSampleRate(LiveConfig.AUDIO_SAMPLE_RATE_44100)// 采样率
                .setGopLengthInSeconds(2).build();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            // 硬编码
            liveSession = new LiveSessionHW(this, liveConfig);
        } else {
            liveSession = new LiveSessionSW(this, liveConfig);
        }
        liveSession.setStateListener(new SessionStateListener() {
            @Override
            public void onSessionPrepared(int i) {

            }

            @Override
            public void onSessionStarted(int i) {
                // 启动该
            }

            @Override
            public void onSessionStopped(int i) {

            }

            @Override
            public void onSessionError(int i) {

            }
        });
        liveSession.bindPreviewDisplay(sfv.getHolder());
        liveSession.prepareSessionAsync();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
